import axios from "axios";
import {router} from '../router'


const service = axios.create({
    // @ts-ignore
    baseURL: import.meta.env.VITE_APP_BASE_API, // url = base url + request url
    // withCredentials: true, // send cookies when cross-domain requests
    withCredentials: false,
    timeout: 90000 // request timeout
})

service.interceptors.request.use(
    config => {
        // do something before request is sent
        return config
    },
    error => {
        // do something with request error
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    /**
     * 如果接口返回的状态码不是200, 则认为接口发生错误
     * 接口发生错误时, 根据状态码做出如下默认处理:
     * 500 提示错误信息
     * 401 返回登录叶
     * 403 提示没有访问权限
     */
    response => {
        return response.data
    },
    error => {
        const errResponse = error.response
        const status = errResponse.status
        return Promise.reject(error)
    }
)

export default service;
